local qiaobian = fk.CreateSkill {

  name = "joy__qiaobian",

  tags = {  },

}



qiaobian:addEffect(fk.EventPhaseChanging, {
  name = "joy__qiaobian",
  anim_type = "offensive",
  events = {fk.EventPhaseChanging,fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    if event == fk.EventPhaseStart then
      return target == player and player.phase == Player.Finish and player:hasSkill(qiaobian.name) and player:getMark("joy__qiaobian-turn") > 2 and not player.dead
    else
      return target == player and player:hasSkill(qiaobian.name) and not player:isKongcheng() and
    data.to > Player.Start and data.to < Player.Finish
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.EventPhaseStart then return true end
    local phase_name_table = {
      [3] = "phase_judge",
      [4] = "phase_draw",
      [5] = "phase_play",
      [6] = "phase_discard",
    }
    local card = player.room:askForDiscard(player, 1, 1, false, qiaobian.name, true, ".", "#joy__qiaobian-invoke:::" .. phase_name_table[data.to], true)
    if #card > 0 then
      event:setCostData(self, card)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      player:drawCards(2,qiaobian.name)
    else
    room:throwCard(event:getCostData(self), qiaobian.name, player, player)
    player:skip(data.to)
    room:addPlayerMark(player,"joy__qiaobian-turn",1)
    if data.to == Player.Draw then
      local targets = table.map(table.filter(room:getOtherPlayers(player), function(p)
        return not p:isKongcheng() end), Util.IdMapper)
      if #targets > 0 then
        local n = math.min(2, #targets)
        local tos = room:askForChoosePlayers(player, targets, 1, n, "#joy__qiaobian-choose:::"..n, qiaobian.name, true)
        if #tos > 0 then
          room:sortPlayersByAction(tos)
          for _, id in ipairs(tos) do
            local p = room:getPlayerById(id)
            if not p:isKongcheng() then
              local card_id = room:askForCardChosen(player, p, "h", qiaobian.name)
              room:obtainCard(player, card_id, false, fk.ReasonPrey)
            end
          end
        end
      end
    elseif data.to == Player.Play then
      local targets = room:askForChooseToMoveCardInBoard(player, "#joy__qiaobian-move", qiaobian.name, true, nil)
      if #targets ~= 0 then
        targets = table.map(targets, function(id) return room:getPlayerById(id) end)
        room:askForMoveCardInBoard(player, targets[1], targets[2], qiaobian.name)
      end
    end
    return true
    end
  end,
})
qiaobian:addEffect(fk.EventPhaseStart, {
  name = "joy__qiaobian",
  anim_type = "offensive",
  events = {fk.EventPhaseChanging,fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    if event == fk.EventPhaseStart then
      return target == player and player.phase == Player.Finish and player:hasSkill(qiaobian.name) and player:getMark("joy__qiaobian-turn") > 2 and not player.dead
    else
      return target == player and player:hasSkill(qiaobian.name) and not player:isKongcheng() and
    data.to > Player.Start and data.to < Player.Finish
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.EventPhaseStart then return true end
    local phase_name_table = {
      [3] = "phase_judge",
      [4] = "phase_draw",
      [5] = "phase_play",
      [6] = "phase_discard",
    }
    local card = player.room:askForDiscard(player, 1, 1, false, qiaobian.name, true, ".", "#joy__qiaobian-invoke:::" .. phase_name_table[data.to], true)
    if #card > 0 then
      event:setCostData(self, card)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      player:drawCards(2,qiaobian.name)
    else
    room:throwCard(event:getCostData(self), qiaobian.name, player, player)
    player:skip(data.to)
    room:addPlayerMark(player,"joy__qiaobian-turn",1)
    if data.to == Player.Draw then
      local targets = table.map(table.filter(room:getOtherPlayers(player), function(p)
        return not p:isKongcheng() end), Util.IdMapper)
      if #targets > 0 then
        local n = math.min(2, #targets)
        local tos = room:askForChoosePlayers(player, targets, 1, n, "#joy__qiaobian-choose:::"..n, qiaobian.name, true)
        if #tos > 0 then
          room:sortPlayersByAction(tos)
          for _, id in ipairs(tos) do
            local p = room:getPlayerById(id)
            if not p:isKongcheng() then
              local card_id = room:askForCardChosen(player, p, "h", qiaobian.name)
              room:obtainCard(player, card_id, false, fk.ReasonPrey)
            end
          end
        end
      end
    elseif data.to == Player.Play then
      local targets = room:askForChooseToMoveCardInBoard(player, "#joy__qiaobian-move", qiaobian.name, true, nil)
      if #targets ~= 0 then
        targets = table.map(targets, function(id) return room:getPlayerById(id) end)
        room:askForMoveCardInBoard(player, targets[1], targets[2], qiaobian.name)
      end
    end
    return true
    end
  end,
})

return qiaobian